import axios from "axios"
import qs from 'qs'
import { Message } from 'element-ui';

// const accessToken = sessionStorage.getItem('accessToken')
let Axios = axios.create({
	responseType: 'json',
	// timeout: 3000000,
	headers: {
		// 'token': accessToken ? accessToken : ''
		'Content-Type': 'application/json;charset=utf-8',
		// 'Accept': 'application/json'
	}
})

// 添加响应拦截器
Axios.interceptors.response.use(function (response) {
	let resp = response.data
	// 对响应数据做点什么  
	if (response.status === 200) {
		// 判断是401重定向
		if (resp.code === 401) {
			if (resp.data.location) {
				window.location.href = location + '?redirect_url=' + encodeURIComponent(window.location.href)
			}
		} else if (resp.code !== 200) {
			// 操作错误
			Message.error(resp.message);
			return Promise.reject(resp.data)
		}
	}
	return resp
}, function (error) {
	// 抛出错误
	Message.error(error.message)
	return Promise.reject(error)
}
)



/**
 * get方法，对应get请求
 * @param {String} url [请求的url地址]
 * @param {Object} params [请求时携带的参数]
 */
export const get = (url, params) => {
	const accessToken = sessionStorage.getItem('token')
	return Axios.get(url, { params }, {
		headers: {
			'token': accessToken ? accessToken : ''
		}
	});
};

/**
 * post方法，对应query传参的post请求
 * @param {String} url [请求的url地址]
 * @param {Object} params [请求时携带的参数]
 */
export const postQuery = (url, params) => {
	const accessToken = sessionStorage.getItem('token')
	return Axios.post(url, null, {
		params: params,
		headers: {
			'token': accessToken ? accessToken : ''
		}
	})
};

/**
 * post方法，对应json传参的post请求
 * @param {String} url [请求的url地址]
 * @param {Object} data [请求时携带的参数]
 */
export const post = (url, data) => {
	const accessToken = sessionStorage.getItem('token')
	return Axios.post(url, data, {
		headers: {
			'token': accessToken ? accessToken : ''
		}
	})
};

/**
 * post方法，对应formdata传参的post请求
 * @param {String} url [请求的url地址]
 * @param {Object} data [请求时携带的参数]
 */
export const postForm = (url, data, opt) => {
	const accessToken = sessionStorage.getItem('token')
	return Axios.post(url, data, {
		headers: {
			'Content-Type': 'application/x-www-form-urlencoded;charset=utf-8',
			'X-Requested-With': 'XMLHttpRequest',
			'token': accessToken ? accessToken : ''
		},
		transformRequest: [function (val) {
			return qs.stringify(val)
		}],
		...opt
	});
};

/**
 * post方法，对应上传file的post请求
 * @param {String} url [请求的url地址]
 * @param {Object} params [请求时携带的参数]
*/
export const postUpload = (url, param) => {
	const accessToken = sessionStorage.getItem('token')
	const config = {
		headers: { 'Content-Type': 'multipart/form-data', 'token': accessToken ? accessToken : '' }
	}
	return Axios.post(url, param, config)
};

/**
 * put方法，对应put请求
 * @param {String} url [请求的url地址]
 * @param {Object} params [请求时携带的参数]
 */
export const put = (url, params) => {
	const accessToken = sessionStorage.getItem('token')
	return Axios.put(url, params, {
		headers: {
			'token': accessToken ? accessToken : ''
		}
	});
};


export default { get, post, postQuery, postUpload, put, Axios }